package LeetCode热题HOT100;

import java.util.ArrayList;
import java.util.List;

public class p17电话号码的字母组合 {
    List<String> res1=new ArrayList<>();
    public List<String> letterCombinations1(String digits) {
        //回溯算法
        if (digits.length()==0) {
            return res;
        }

        String[] numString={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

        backTrack1(digits,numString,0);

        return res;
    }

    StringBuilder curRes=new StringBuilder();
    private void backTrack1(String digits, String[] numString, int index) {
        //确定终止条件：递归出口
        if (index==digits.length()) {
            res.add(curRes.toString());
            return;
        }

        //确定单层遍历逻辑
        String str=numString[digits.charAt(index)-'0'];//确定当前数字对应的字符串
        //digits.charAt(index)-'0'  字符’1‘-字符’0‘=数字1

        for (int i = 0; i <str.length(); i++) {
            curRes.append(str.charAt(i));
            backTrack1(digits,numString,index+1);
            curRes.deleteCharAt(index);
        }
    }


    public static void main(String[] args) {
        String str="1";
        String[] strings={"","abc","def"};
        System.out.println(str.charAt(0)-'0');
//        System.out.println('1'+1);
//        String str2=strings[str.charAt(0)];
//        System.out.println(str2);
    }


    List<String> res=new ArrayList<>();
    public List<String> letterCombinations(String digits) {
        if (digits.length()==0) {
            return res;
        }

        String[] intString={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

        backTrack(digits,intString,0);
        return res;

    }

    StringBuilder curStirng=new StringBuilder();
    private void backTrack(String digits, String[] intString, int index) {
        //确定递归终止条件
        if (index==digits.length()) {
            res.add(curStirng.toString());
            return;
        }

        String str=intString[digits.charAt(index)-'0'];

        //确定单层递归逻辑
        for (int i = 0; i <str.length() ; i++) {
            curStirng.append(str.charAt(i));
            backTrack(digits,intString,index+1);
            curStirng.deleteCharAt(index);
        }
    }


}
